{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.16 *args\u548c**kwargs\u7684\u5f3a\u5236\u53c2\u6570\u7b7e\u540d\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u5b83\u4f7f\u7528*args\u548c**kwargs\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd9\u6837\u4f7f\u5f97\u5b83\u6bd4\u8f83\u901a\u7528\uff0c\n\u4f46\u6709\u65f6\u5019\u4f60\u60f3\u68c0\u67e5\u4f20\u9012\u8fdb\u6765\u7684\u53c2\u6570\u662f\u4e0d\u662f\u67d0\u4e2a\u4f60\u60f3\u8981\u7684\u7c7b\u578b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4efb\u4f55\u6d89\u53ca\u5230\u64cd\u4f5c\u51fd\u6570\u8c03\u7528\u7b7e\u540d\u7684\u95ee\u9898\uff0c\u4f60\u90fd\u5e94\u8be5\u4f7f\u7528 inspect \u6a21\u5757\u4e2d\u7684\u7b7e\u540d\u7279\u6027\u3002\n\u6211\u4eec\u6700\u4e3b\u8981\u5173\u6ce8\u4e24\u4e2a\u7c7b\uff1aSignature \u548c Parameter \u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u51fd\u6570\u524d\u9762\u7684\u4ea4\u4e92\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from inspect import Signature, Parameter\n# Make a signature for a func(x, y=42, *, z=None)\nparms = [ Parameter('x', Parameter.POSITIONAL_OR_KEYWORD),\n        Parameter('y', Parameter.POSITIONAL_OR_KEYWORD, default=42),\n        Parameter('z', Parameter.KEYWORD_ONLY, default=None) ]\nsig = Signature(parms)\nprint(sig)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u6709\u4e86\u4e00\u4e2a\u7b7e\u540d\u5bf9\u8c61\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684 bind() \u65b9\u6cd5\u5f88\u5bb9\u6613\u7684\u5c06\u5b83\u7ed1\u5b9a\u5230 *args \u548c **kwargs \u4e0a\u53bb\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6f14\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def func(*args, **kwargs):\n    bound_values = sig.bind(*args, **kwargs)\n    for name, value in bound_values.arguments.items():\n        print(name,value)\n# Try various examples\nfunc(1, 2, z=3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(1, z=3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(y=2, x=1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(1, 2, 3, 4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(y=2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(1, y=2, x=3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u901a\u8fc7\u5c06\u7b7e\u540d\u548c\u4f20\u9012\u7684\u53c2\u6570\u7ed1\u5b9a\u8d77\u6765\uff0c\u53ef\u4ee5\u5f3a\u5236\u51fd\u6570\u8c03\u7528\u9075\u5faa\u7279\u5b9a\u7684\u89c4\u5219\uff0c\u6bd4\u5982\u5fc5\u586b\u3001\u9ed8\u8ba4\u3001\u91cd\u590d\u7b49\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u5f3a\u5236\u51fd\u6570\u7b7e\u540d\u66f4\u5177\u4f53\u7684\u4f8b\u5b50\u3002\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u57fa\u7c7b\u4e2d\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u975e\u5e38\u901a\u7528\u7684 __init__() \u65b9\u6cd5\uff0c\n\u7136\u540e\u6211\u4eec\u5f3a\u5236\u6240\u6709\u7684\u5b50\u7c7b\u5fc5\u987b\u63d0\u4f9b\u4e00\u4e2a\u7279\u5b9a\u7684\u53c2\u6570\u7b7e\u540d\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from inspect import Signature, Parameter\n\ndef make_sig(*names):\n    parms = [Parameter(name, Parameter.POSITIONAL_OR_KEYWORD)\n            for name in names]\n    return Signature(parms)\n\nclass Structure:\n    __signature__ = make_sig()\n    def __init__(self, *args, **kwargs):\n        bound_values = self.__signature__.bind(*args, **kwargs)\n        for name, value in bound_values.arguments.items():\n            setattr(self, name, value)\n\n# Example use\nclass Stock(Structure):\n    __signature__ = make_sig('name', 'shares', 'price')\n\nclass Point(Structure):\n    __signature__ = make_sig('x', 'y')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4f7f\u7528\u8fd9\u4e2a Stock \u7c7b\u7684\u793a\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import inspect\nprint(inspect.signature(Stock))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s1 = Stock('ACME', 100, 490.1)\ns2 = Stock('ACME', 100)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s3 = Stock('ACME', 100, 490.1, shares=50)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6211\u4eec\u9700\u8981\u6784\u5efa\u901a\u7528\u51fd\u6570\u5e93\u3001\u7f16\u5199\u88c5\u9970\u5668\u6216\u5b9e\u73b0\u4ee3\u7406\u7684\u65f6\u5019\uff0c\u5bf9\u4e8e *args \u548c **kwargs \u7684\u4f7f\u7528\u662f\u5f88\u666e\u904d\u7684\u3002\n\u4f46\u662f\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u6709\u4e00\u4e2a\u7f3a\u70b9\u5c31\u662f\u5f53\u4f60\u60f3\u8981\u5b9e\u73b0\u81ea\u5df1\u7684\u53c2\u6570\u68c0\u9a8c\u65f6\uff0c\u4ee3\u7801\u5c31\u4f1a\u7b28\u62d9\u6df7\u4e71\u3002\u57288.11\u5c0f\u8282\u91cc\u9762\u6709\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\u3002\n\u8fd9\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u7b7e\u540d\u5bf9\u8c61\u6765\u7b80\u5316\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6700\u540e\u7684\u4e00\u4e2a\u65b9\u6848\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u81ea\u5b9a\u4e49\u5143\u7c7b\u6765\u521b\u5efa\u7b7e\u540d\u5bf9\u8c61\u3002\u4e0b\u9762\u6f14\u793a\u600e\u6837\u6765\u5b9e\u73b0\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from inspect import Signature, Parameter\n\ndef make_sig(*names):\n    parms = [Parameter(name, Parameter.POSITIONAL_OR_KEYWORD)\n            for name in names]\n    return Signature(parms)\n\nclass StructureMeta(type):\n    def __new__(cls, clsname, bases, clsdict):\n        clsdict['__signature__'] = make_sig(*clsdict.get('_fields',[]))\n        return super().__new__(cls, clsname, bases, clsdict)\n\nclass Structure(metaclass=StructureMeta):\n    _fields = []\n    def __init__(self, *args, **kwargs):\n        bound_values = self.__signature__.bind(*args, **kwargs)\n        for name, value in bound_values.arguments.items():\n            setattr(self, name, value)\n\n# Example\nclass Stock(Structure):\n    _fields = ['name', 'shares', 'price']\n\nclass Point(Structure):\n    _fields = ['x', 'y']"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6211\u4eec\u81ea\u5b9a\u4e49\u7b7e\u540d\u7684\u65f6\u5019\uff0c\u5c06\u7b7e\u540d\u5b58\u50a8\u5728\u7279\u5b9a\u7684\u5c5e\u6027 __signature__ \u4e2d\u901a\u5e38\u662f\u5f88\u6709\u7528\u7684\u3002\n\u8fd9\u6837\u7684\u8bdd\uff0c\u5728\u4f7f\u7528 inspect \u6a21\u5757\u6267\u884c\u5185\u7701\u7684\u4ee3\u7801\u5c31\u80fd\u53d1\u73b0\u7b7e\u540d\u5e76\u5c06\u5b83\u4f5c\u4e3a\u8c03\u7528\u7ea6\u5b9a\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import inspect\nprint(inspect.signature(Stock))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(inspect.signature(Point))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}